//
//  AFTool.m
//  JinYong_iOS_Store
//
//  Created by 王厚一 on 2017/10/17.
//  Copyright © 2017年 why. All rights reserved.
//

#import "AFTool.h"
#import "AFHTTPSessionManager.h"
#import "MBProgressHUD+NJ.h"
@implementation AFTool

+ (void)getWithURL:(NSString *)url params:(NSDictionary *)params success:(successBlock)success failure:(failureBlock)failure
{
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
//    manager.responseSerializer = [AFJSONResponseSerializer serializer];
     manager.responseSerializer.acceptableContentTypes = [manager.responseSerializer.acceptableContentTypes setByAddingObject:@"text/html"];
    [manager GET:url parameters:params progress:^(NSProgress * _Nonnull downloadProgress) {
    } success:^(NSURLSessionDataTask * task, id responseObject) {
        [MBProgressHUD hideHUD];
        if (success) {
            success(responseObject);
        }
    } failure:^(NSURLSessionDataTask * task, NSError * error) {
        NSLog(@"%@",error);
//        [MBProgressHUD showError:[NSString stringWithFormat:@"网络错误,%@", error.userInfo[@"NSErrorFailingURLKey"]]];
        if (failure) {
            failure(error);
        }
    }];
}

+ (void)postWithURL:(NSString *)url params:(NSDictionary *)params success:(successBlock)success failure:(failureBlock)failure
{
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
//    manager.responseSerializer = [AFJSONResponseSerializer serializer];
    manager.responseSerializer.acceptableContentTypes = [manager.responseSerializer.acceptableContentTypes setByAddingObject:@"text/html"];
    [manager POST:url parameters:params progress:^(NSProgress * _Nonnull uploadProgress) {
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        [MBProgressHUD hideHUD];
        if (success) {
            success(responseObject);
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        [MBProgressHUD hideHUD];
         NSLog(@"%@",error);
//        [MBProgressHUD showError:[NSString stringWithFormat:@"网络错误,%@", error.userInfo[@"NSErrorFailingURLKey"]]];
        if (failure) {
            failure(error);
        }
    }];
}

+ (void)postDataWithURL:(NSString *)url params:(NSDictionary *)params data:(NSData *)data imageName:(NSString *)imageName success:(successBlock)success failure:(failureBlock)failure
{
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
//    manager.responseSerializer = [AFJSONResponseSerializer serializer];
     manager.responseSerializer.acceptableContentTypes = [manager.responseSerializer.acceptableContentTypes setByAddingObject:@"text/html"];
    [manager POST:url parameters:params constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {
    
        [formData appendPartWithFileData:data name:@"pic" fileName:@"pic" mimeType:@"image/jpeg"];
        
    } progress:^(NSProgress * _Nonnull uploadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        [MBProgressHUD hideHUD];
        if (success) {
            success(responseObject);
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        [MBProgressHUD hideHUD];
//        [MBProgressHUD showError:[NSString stringWithFormat:@"网络错误,%@", error.userInfo[@"NSErrorFailingURLKey"]]];
        if (failure) {
            failure(error);
        }
    }];
}

+ (void)postDataWithURL:(NSString *)url params:(NSDictionary *)params datas:(NSArray *)datas imageName:(NSString *)imageName success:(successBlock)success failure:(failureBlock)failure
{
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.responseSerializer.acceptableContentTypes = [manager.responseSerializer.acceptableContentTypes setByAddingObject:@"text/html"];
    [manager POST:url parameters:params constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {
        for (int i = 0; i< datas.count; i++) {
            NSString *name = [NSString stringWithFormat:@"name%d", i];
            NSString *fileName = [NSString stringWithFormat:@"fileName%d.jpg", i];
            [formData appendPartWithFileData:datas[i] name:name fileName:fileName mimeType:@"image/jpg"];
        }
    } progress:^(NSProgress * _Nonnull uploadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        [MBProgressHUD hideHUD];
        if (success) {
            success(responseObject);
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        [MBProgressHUD hideHUD];
//        [MBProgressHUD showError:[NSString stringWithFormat:@"网络错误,%@", error.userInfo[@"NSErrorFailingURLKey"]]];
        if (failure) {
            failure(error);
        }
    }];
}


@end
